$(function () {
    let userid=localStorage.getItem("userId");
    findById(userid);
})
function findById(id) {
    let data=myAjax("/user/finById",{id:id},"post");
    setData(data.userModel);
}
function setData(data) {
    $("#user-name").val(data.username);
    $("#phone").val(data.phone);
    $("input[name='sex'][value="+data.sex+"]").attr("checked",true);
    $("#birthday").val(data.birthday);
    $('input:checkbox[name=hobby]').each(function () {
        let string=data.hobby;
        string=string.replace("[","")
            .replace("]","")
            .replace("\"","")
            .replace("\"","")
            .replace("\"","")
            .replace("\"","");
        let hobby = string.split(",");
            for (let i = 0; i < hobby.length; i++){
                // console.log($(this).val(),(hobby[i]));
                if ($(this).val() == $.trim(hobby[i])){
                    $(this).attr('checked', true);
                    break;
                }
            }
    });
    $("#introduce").val(data.introduce);
}

function clealAlll() {
    $("#user-name").val("");
    $("#phone").val("");
    $("input[name='sex']").attr("checked",false);
    // $("input:radio[name='sex']").attr("checked",false),
    $("#birthday").val("");
    $('input:checkbox[name=hobby]').each(function () {
        $(this).attr('checked', false);
    });
    $("#introduce").val("");
}

function  save() {
    // js数据类型 数字，数组，字符串，布尔，对象
    let data={
        id:localStorage.getItem("userId"),
        username:$("#user-name").val(),
        phone:$("#phone").val(),
        sex:$("input[name='sex']:checked").val(),
        birthday:$("#birthday").val(),
        hobby:JSON.stringify((aihao())),
        introduce:$("#introduce").val(),
    };
    let da=myAjax("/user/update",data,"post");
    if (da.result==1){
        alert("修改成功");
        $('#reght').load('../back/userManage.html')
    }else {
        alert("修改失败");
    }
}

function aihao() {
    let hb=[];
    $("input:checkbox[name='hobby']").each(function (){
        if ($(this).prop("checked")) {
            hb.push($(this).val());
        }
    })
    return hb;
}

function namess() {
    let username=$("#user-name").val();
    let regaa=/[a-zA-Z]{5,15}$/;
    if (!regaa.test(username)) {
        alert("你输入的用户名格式不正确");
    }
}







